import requests as rq
from bs4 import BeautifulSoup as bs
	
class Cartons(object):
	def __init__(self,title,url):
		self.title=title
		self.url='http://www.imomoe.ai'+url
		self.jsurl=''
		self.len=0
		self.dlurls={}
		
	def get_js(self,testurl,headers2):
		print(testurl)
		res=rq.get(url=testurl,headers=headers2)
		res.encoding=res.apparent_encoding
		soup2=bs(res.text,'lxml')
		soup2=soup2.find_all('div',class_='pp')
		soup2=soup2[0].find_all('div',class_='player')
		soup2=soup2[0].find_all('script')
		self.jsurl='http://www.imomoe.ai'+soup2[0]['src']
		
	def get_dlurl(self,headers2):
		res=rq.get(url=self.jsurl,headers=headers2)
		res.encoding='gbk'
		print(type(res.text))
		x=res.text.split('[')[3]
		x=x.split('\',\'')
		for i in x:
			self.dlurls[i.split('$')[0][-14:]]=i.split('$')[1]

	def download(self,headers2):
		num=1
		for key,value in self.dlurls.items():
			print('开始下载')
			res=rq.get(url=value,headers=headers2)
			with open(str(num)+'.mp4','wb') as fp:
				fp.write(res.content)
				print('下载完成')
			num=num+1
				
	
	
	
url='http://www.imomoe.ai/search.asp'
data={'searchword':input('番名：').encode('gbk')}
headers1={"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control":'max-age=0',
"Connection": "keep-alive",
"Content-Length": "17",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "UM_distinctid=1784aeb82f224d-0571be1be33e2e-5319301f-1fa400-1784aeb82f31ee; CNZZDATA1260742008=1557275411-1616164293-null%7C1616164293; Hm_lvt_38c112aee0c8dc4d8d4127bb172cc197=1616164652; ASPSESSIONIDQQQDCASQ=EEPEIMOBPDCFGPHNNDKIFIEM; Hm_lvt_eaa57ca47dacb4ad4f5a257001a3457c=1616164695; Hm_lpvt_38c112aee0c8dc4d8d4127bb172cc197=1616164792; Hm_lpvt_eaa57ca47dacb4ad4f5a257001a3457c=1616164808",
"Host": "www.imomoe.ai",
"Origin": "http://www.imomoe.ai",
"Referer": "http://www.imomoe.ai/search.asp",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
}
headers2={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"}

response=rq.post(url,headers=headers1,data=data)
response.encoding=response.apparent_encoding
soup=bs(response.text,'lxml')
x=soup.find_all('div',class_='pics')
soup=x[0]
leaves=soup.find_all('li')
len0=len(leaves)
for i in range(0,len0):
	print(i,leaves[i].h2.a['title'])
daihao=int(input('代号：'))
carton=Cartons(leaves[daihao].h2.a['title'],leaves[daihao].h2.a['href'])
print(carton.title,carton.url)
response1=rq.get(url=carton.url,headers=headers2)
response1.encoding=response1.apparent_encoding
soup1=bs(response1.text,'lxml')
leaves1=soup1.find_all('div',class_='movurl')
leaves1=leaves1[0].ul
leaves1=leaves1.find_all('li')
carton.len=len(leaves1)
print(carton.len)
testurl='http://www.imomoe.ai'+leaves1[0].a['href']
print(testurl)
carton.get_js(testurl,headers2)
print(carton.jsurl)
carton.get_dlurl(headers2)
print(carton.dlurls)
carton.download(headers2)










